//
//  YYECommentViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/3.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYECommentViewController.h"
#import "YYEIntrolCommandCell.h"
#import "YYELessonCommandModel.h"

@interface YYECommentViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) YYEBaseTableView *tableView;
@property (nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,assign) NSInteger page;

@end

@implementation YYECommentViewController

static NSString *commandCell = @"commandCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 评论
//    self.view.backgroundColor = [UIColor blueColor];
    
    [self configUI];
 
    self.page = 1;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [header beginRefreshing];
    self.tableView.mj_header = header;

    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer = footer;
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = GeneralBackColor;
    self.tableView.tableHeaderView = headerView;

}

- (void)loadNewData {
    
    [self loadDataWithPage:self.page];
    
}

- (void)loadMoreData {
    
    self.page ++;
    [self loadDataWithPage:self.page];
    
}

- (void)loadDataWithPage:(NSInteger)page {
    
    NSDictionary *dic = @{
                          @"cid":self.cid,
                          @"p":@(self.page),
                          @"size":@10,
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token
                          };
    [SVProgressHUD show];
    [AFNHelper post:LessonComments parameters:dic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if (self.page == 1) {
            [self.datas removeAllObjects];
        }
        if ([responseObject[@"status"] isEqual:@1]) {
            
            NSArray *commandArr = [YYELessonCommandModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.datas addObjectsFromArray:commandArr];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        NSInteger totalPage = [responseObject[@"total_page"] integerValue];
        
        if (totalPage == self.page) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD showWithStatus:ShowError];
        [SVProgressHUD dismissWithDelay:0.5];
        
    }];
    
}

- (void)configUI {
    
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[YYEIntrolCommandCell class] forCellReuseIdentifier:commandCell];
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
    
}

- (YYEBaseTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[YYEBaseTableView alloc]initwithDelegate:self];
    }
    return _tableView;
    
}

- (NSMutableArray *)datas {
    
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
    
}

#pragma mark --  UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.datas.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYEIntrolCommandCell *cell = [tableView dequeueReusableCellWithIdentifier:commandCell];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.commandModel = self.datas[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YYELessonCommandModel *model = self.datas[indexPath.row];
    
    CGSize sizeForLabel = [model.comment boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 36, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
    
    return sizeForLabel.height + 55;
    // 55 ziti 13
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
